/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package net.tygzs.easygem.configuration;

import cn.citycraft.Multkills.config.FileConfig;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.milkbowl.vault.economy.Economy;
import net.tygzs.easygem.Main;
import static org.bukkit.Bukkit.getServer;
import org.bukkit.plugin.RegisteredServiceProvider;

/**
 * 插件配置类
 *
 * @author thtTNT
 */
public class PluginConfig
{

    private final Map<String, String> message = new HashMap<String, String>();
    private final ID id = new ID();
    private final DefaultConfig defaultConfig = new DefaultConfig();
    private final DropConfig dropConfig = new DropConfig();
    private Economy economy;

    public PluginConfig()
    {
        String language = Main.plugin.getConfig().getString("language");
        File file = new File(Main.plugin.getDataFolder(), "language/" + language + ".yml");
        initMessage(file);
        setupEconomy();
    }

    /**
     * 获取语言类
     *
     * @return 语言类
     */
    public Map getMessage()
    {
        return this.message;
    }

    /**
     * 初始化语言文件
     *
     * @param file 语言文件目录
     */
    private void initMessage(File file)
    {
        message.put("Name_RepairStone", "§a修复石");
        message.put("Help_give", "§a/EasyGem give [类型] [数量] <玩家>给指定玩家指定数量的宝石");
        message.put("NoNumber", "§4您还没有输入数字呢");
        message.put("Help_give_RepairStone", "§a/EasyGem give RepairStone [数量] <玩家>");
        message.put("Player_notFound", "§4玩家不存在或不在线");
        message.put("Info_give_RepairStone", "§a成功给予[player]了[amount]个修复石");
        message.put("ErrorNumber", "§4您输入了错误的数字");
        message.put("repair_chance", "成功几率");
        message.put("repair_amount", "修复比例");
        message.put("Help_EasyGem", "§a输入/EasyGem 来查询相关帮助");
        message.put("Help_RepairStone_1", "§a/RepairStone chance 设置修复石的发动几率");
        message.put("Help_RepairStone_2", "§a/RepairStone repair 设置修复石的修复比例");
        message.put("NothingInHand", "§4你手上没有拿任何东西");
        message.put("Help_RepairStone_chance", "§a/RepairStone chance [几率] 设置一个修复石修复成功的几率");
        message.put("NotRepairStone", "§4您手上没有拿修复石");
        message.put("Info_RepairStone_chance_set_true", "§a设置几率成功");
        message.put("Error_RepairStone", "§4修复石参数错误");
        message.put("Help_RepairStone_repair", "§a/RepairStone chance [几率] 设置一个修复石的修复比例");
        message.put("Info_RepairStone_repair_set_true", "§a设置修复比例成功");
        message.put("Can'tRepair", "§4该物品无法被修复");
        message.put("Info_RepairStone_use_fail", "§4运气不好,修复失败");
        message.put("Info_RepairStone_use_success","§a修复成功!");
        message.put("Error_noPermission", "§4您没有足够的权限来执行本命令");
        message.put("CheckCode", "校验码");
        if (!file.exists())
        {
            file = new File(Main.plugin.getDataFolder(), "language");
            if (!file.exists())
            {
                file.mkdirs();
            }
            file = new File(Main.plugin.getDataFolder(), "language/chinese.yml");
            if (!file.exists())
            {
                try
                {
                    Set<String> keys = message.keySet();
                    file.createNewFile();
                    FileConfig config = FileConfig.loadConfiguration(file);
                    for (String key : keys)
                    {
                        config.set(key, ((String) message.get(key)).replace("§", "&"));
                    }
                    config.save(file);
                    return;
                } catch (IOException ex)
                {
                    Logger.getLogger(PluginConfig.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        FileConfig config = FileConfig.loadConfiguration(file);
        Set<String> keys = config.getKeys(true);
        for (String key : keys)
        {
            message.put(key, config.getString(key).replace("&", "§"));
        }
    }

    /**
     * ID类
     */
    public class ID
    {

        private final int RepairStone;

        public ID()
        {
            this.RepairStone = Main.plugin.getConfig().getInt("RepairStone.id");
        }

        public int getRepairStone()
        {
            return this.RepairStone;
        }
    }

    /**
     * 获取ID类
     *
     * @return ID类
     */
    public ID getID()
    {
        return this.id;
    }

    public class DefaultConfig
    {

        private final RepairStone repairStone = new RepairStone();

        public class RepairStone
        {

            public final int chance = Main.plugin.getConfig().getInt("RepairStone.default_chance");
            public final int amount = Main.plugin.getConfig().getInt("RepairStone.default_repair");
            public final short key = (short) Main.plugin.getConfig().getInt("RepairStone.Gem_key");
        }

        /**
         * 获取修复石默认值
         *
         * @return 获取修复石默认值
         */
        public RepairStone getRepairStone()
        {
            return this.repairStone;
        }
    }

    /**
     * 获取插件默认值
     *
     * @return 插件默认值
     */
    public DefaultConfig getDefaultConfig()
    {
        return this.defaultConfig;
    }

    /**
     * 加载经济插件
     */
    private boolean setupEconomy()
    {
        RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
        if (economyProvider != null)
        {
            this.economy = economyProvider.getProvider();
        }
        return (this.economy != null);
    }

    /**
     * 掉落配置类
     */
    public class DropConfig
    {

        private final RepairStone repairConfig = new RepairStone();

        public class RepairStone
        {

            public double zombie = Main.plugin.getConfig().getDouble("Drop.RepairStone.Zombie");
            public double skeleton = Main.plugin.getConfig().getDouble("Drop.RepairStone.Skeleton");
            public double blaze = Main.plugin.getConfig().getDouble("Drop.RepairStone.Blaze");
            public double Creeper = Main.plugin.getConfig().getDouble("Drop.RepairStone.Creeper");
            public double Ghast = Main.plugin.getConfig().getDouble("Drop.RepairStone.Ghast");
            public double Cow=Main.plugin.getConfig().getDouble("Drop.RepairStone.Cow");
            public double Sheep=Main.plugin.getConfig().getDouble("Drop.RepairStone.Sheep");
            public double Pig=Main.plugin.getConfig().getDouble("Drop.RepiarStone.Pig");
            public double EnderMan=Main.plugin.getConfig().getDouble("Drop.RepairStone.EnderMan");
            public double EnderDragon=Main.plugin.getConfig().getDouble("Drop.RepairStone.EnderDragon");
            public double Slime=Main.plugin.getConfig().getDouble("Drop.RepairStone.Slime");
        }

        public RepairStone getRepairStone()
        {
            return this.repairConfig;
        }
    }

    /**
     * 获取掉落配置类
     *
     * @return 掉落配置类
     */
    public DropConfig getDropConfig()
    {
        return this.dropConfig;
    }
}
